package com.wyh.mapper.gfivemapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.wyh.common.result.FindAccountResult;
import com.wyh.common.result.SysAccountManagementResult;
import com.wyh.domain.po.gfive.PageHelper;
import com.wyh.domain.po.gfive.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface UserMapper extends BaseMapper<User> {
//    MasterDataUser getUserByCustomerId(MasterDataContract contract);
    /**
     * 系统管理->账号管理-》分页查看所有账号
     * @param pageHelper
     * @return
     */
    @Select("SELECT user.id, user.name, user.telephone, user.state,user.account,organization.id AS organizationid,organization.name AS organizationName,GROUP_CONCAT(role.name SEPARATOR ';') AS roleName FROM user " +
            "INNER JOIN organization ON user.organizationid = organization.id AND organization.state=1 " +
            "INNER JOIN user_role ON user.id = user_role.userid AND user_role.state =1 " +
            "INNER JOIN `role` ON user_role.roleid = role.id AND role.state=1 " +
            "GROUP BY user.id, user.name, user.telephone, user.state, organization.name " +
            "LIMIT #{currentPage}, #{pageSize}")
    List<SysAccountManagementResult> findAll(PageHelper pageHelper);


    User selectByUsername(String username);

    /**
     * 根据id查找用户的手机号
     * @param id
     * @return
     */
    @Select("select `user`.telephone from `user` where `user`.id=#{id}")
    String selectUserTelephoneById(int id);

    /**
     * 重置密码为手机号后6位
     * @param user
     * @return
     */
    @Update("update user set password=#{password} where id=#{id}")
    int restPassword(User user);

    User findByPhone(String phone);

    /**
     * 新增账号
     */
    @Insert("INSERT INTO user (name, telephone,organizationid,state,account, password)" +
            "values (#{name},#{telephone},#{organizationid},1,#{account},#{password})")
    int saveAccount(User user);

    /**
     * 根据账号查User的id
     * 根据手机号和公司编号查询
     */
    @Select("select `user`.id from `user` where `user`.organizationid = #{organizationid} AND `user`.telephone = #{telephone}")
    User findUserId(User user);

    /**
     * 根据组织查Account
     */
    @Select("SELECT `user`.account from `user` where `user`.organizationid = #{organization}")
    List<User> findAccountByOrganization(int organization);
    /**
     *查询账号专用
     */
    List<FindAccountResult> findUsers(FindAccountResult findAccountResult);
    /**
     * 根据id查账号
     */
    @Select("select * from user where id =#{id}")
    User findById(Integer id);
    /**
     * 更新账号
     */
    int updateUser(User user);

    @Update("update  user set password=#{newPwd} where id=#{id}")
    void updatePwd(@Param("newPwd") String newPwd,@Param("id") Integer id);

    @Update("update user set state=0 where account=#{account}")
    void updateState(String account);

    @Select("select user.id from user where user.account = #{account}")
    int[] FindUserIds(String account);
}
